package com.whut.mobilesafe.engine;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.inputmethod.InputConnection;

import com.whut.mobilesafe.domain.AppInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 作    者:yubo.xiaoyubo
 * 时    间：2015/12/28 9:22
 * 版    本：1.0
 * 描    述： 应用信息提供者
 */
public class AppInfoProvider {


    public static ArrayList<AppInfo> getAllApp(Context context) {
        ArrayList<AppInfo> appInfos = new ArrayList<>();

        PackageManager packageManager = context.getPackageManager();
        List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(packageManager.GET_UNINSTALLED_PACKAGES);

        for (ApplicationInfo info : installedApplications) {

            String name = info.loadLabel(packageManager).toString();
            Drawable icon = info.loadIcon(packageManager);

            AppInfo appInfo = new AppInfo();
            appInfo.setName(name);
            appInfo.setIcon(icon);

            // 状态机, 通过01状态来表示是否具备某些属性和功能
            int flag = info.flags;
            if ((flag & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE) {
                appInfo.setPosition("SD卡存储");
            } else {
                appInfo.setPosition("手机内存");
            }

            if ((flag & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo.FLAG_SYSTEM) {
                appInfo.setIsUser(false);
            } else {
                appInfo.setIsUser(true);
            }

            appInfos.add(appInfo);
        }

        return appInfos;
    }
}
